package com.hgd.security.evp.biz.biz;

import com.alibaba.fastjson.JSONObject;
import com.hgd.security.evp.biz.entity.EvpGroup;
import com.hgd.security.evp.biz.mapper.EvpGroupMapper;
import com.hgd.security.evp.common.biz.BusinessBiz;
import com.hgd.security.evp.common.util.*;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;

/**
 * @author By Shang
 * @version 2019-01-01 22:49:10
 * @email ${email}
 */
@Service
public class EGroupBiz extends BusinessBiz<EvpGroupMapper, EvpGroup> {
    /**
     * 查询某用户下的组
     *
     * @param userId 用户userId
     * @return 某用户下的组
     */
    public TableResultResponse<JSONObject> getGroupOfUser(Integer userId) {
        TableResultResponse<JSONObject> tableResult = new TableResultResponse<>();
        List<JSONObject> groupOfUser = this.mapper.selectGroupOfUser(userId);
        if (EmptyUtil.isNotEmpty(groupOfUser)) {
            tableResult.setRows(groupOfUser);
        } else {
            tableResult.setRows(new ArrayList<>());
        }

        return tableResult;
    }

    /**
     * 添加单个对象
     * @param evpGroup
     * @return
     */
    public ObjectResultResponse<Void> addEmenu(EvpGroup evpGroup) {
        ObjectResultResponse<Void> oResult=new ObjectResultResponse<>();

        check(evpGroup,oResult,false);
        if(oResult.getStatus()==400){
            return oResult;
        }

        this.insertSelectiveBindCrt(evpGroup);
        oResult.setMessage("添加菜单成功");
        return oResult;
    }

    private void check(EvpGroup groupToOpe, ObjectResultResponse<Void> oResult, boolean isUpdate) {
        /*
         * 1 编号不能重复
         * 2 名称不能重复
         */
        Example example = new Example(EvpGroup.class).selectProperties("id", "evpCode", "evpName");
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("isDeleted", BooleanUtil.BOOLEAN_FALSE);
        criteria.andEqualTo("evpCode", groupToOpe.getEvpCode());

        Example.Criteria criteria1 = example.or();
        criteria1.andEqualTo("isDeleted", BooleanUtil.BOOLEAN_FALSE);
        criteria1.andEqualTo("evpName", groupToOpe.getEvpName());

        List<EvpGroup> evpGroupList = this.selectByExample(example);

        if (EmptyUtil.isNotEmpty(evpGroupList)) {
            if (isUpdate) {
                evpGroupList.forEach(group -> {
                    if (StringUtil.equals(group.getEvpCode(), groupToOpe.getEvpCode()) && !group.getId()
                        .equals(groupToOpe.getId())) {
                        oResult.setStatus(400);
                        oResult.setMessage("权限编号已存在");
                        return;
                    }
                    if (StringUtil.equals(group.getEvpName(), groupToOpe.getEvpName()) && !group.getId()
                        .equals(groupToOpe.getId())) {
                        oResult.setStatus(400);
                        oResult.setMessage("权限名称已存在");
                        return;
                    }
                });
            } else {
                evpGroupList.forEach(group -> {
                    if (StringUtil.equals(group.getEvpCode(), groupToOpe.getEvpCode())) {
                        oResult.setStatus(400);
                        oResult.setMessage("权限编号已存在");
                        return;
                    }
                    if (StringUtil.equals(group.getEvpName(), groupToOpe.getEvpName())) {
                        oResult.setStatus(400);
                        oResult.setMessage("权限名称已存在");
                        return;
                    }
                });
            }
        }
    }

    /**
     * 更新对象
     * @param group
     * @return
     */
    public ObjectResultResponse<Void> updateEGroup(EvpGroup group) {
        ObjectResultResponse<Void> restResult=new ObjectResultResponse<>();

        check(group, restResult, true);
        if(restResult.getStatus()==400){
            return restResult;
        }

        this.updateSelectiveByIdBindUpd(group);

        restResult.setStatus(200);
        restResult.setMessage("编辑权限成功");
        return restResult;
    }

    /**
     * 删除单个对象
     * @param id
     * @return
     */
    public ObjectResultResponse<Void> delEGroup(Integer id) {
        ObjectResultResponse<Void> restResult=new ObjectResultResponse<>();

        EvpGroup group=new EvpGroup();
        group.setId(id);
        group.setIsDeleted(BooleanUtil.BOOLEAN_TRUE);
        this.updateSelectiveByIdBindUpd(group);

        restResult.setMessage("删除权限成功");
        restResult.setStatus(200);
        return restResult;
    }

    /**
     * 分页获取列表
     * @param evpGroup
     * @param page
     * @param rows
     * @return
     */
    public List<JSONObject> getGroupList(EvpGroup evpGroup, int page, int rows) {
        Example example=new Example(EvpGroup.class).selectProperties("id","evpCode","evpName","description");

        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("isDeleted", BooleanUtil.BOOLEAN_FALSE);

        if(StringUtil.isNotBlank(evpGroup.getEvpCode())){
            criteria.andEqualTo("evpCode", evpGroup.getEvpCode());
        }
        if(StringUtil.isNotBlank(evpGroup.getEvpName())){
            criteria.andLike("evpName", "%"+ evpGroup.getEvpName()+"%");
        }

        List<EvpGroup> evpGroups = this.selectByExample(example);

        List<JSONObject> result=new ArrayList<>();
        if(EmptyUtil.isNotEmpty(evpGroups)){
            evpGroups.forEach(group->{
                JSONObject jsonObject = JSONUtil.parseObject(group);
                assert jsonObject != null;
                jsonObject.put("text",group.getEvpName());
                result.add(jsonObject);
            });

            return result;
        }

        return new ArrayList<>();
    }
}